<template>
  <div class="departments-container">
    <div class="app">
      <el-card class="tree-card">
        <!-- 实现头部的结构 -->
        <el-row type="flex" justify="space-between" class="head">
          <el-col>江苏传智科技股份有限公司</el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <el-col> 负责人 </el-col>
              <el-col>
                <el-dropdown>
                  <span>
                    操作
                    <i class="el-icon-arrow-down" />
                  </span>

                  <el-dropdown-menu>
                    <el-dropdown-item>添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>

        <!-- 实现树形结构 -->
        <el-tree
          :data="depts"
          :props="defaultProps"
          default-expand-all
          @addDepts="addDepts"
        >
          <tree-tools
            slot-scope="{ data }"
            :tree-node="data"
            @delDeparts="delDepts"
            @addDepts="addDepts"
            @editDepts="editDepts"
          />
        </el-tree>
      </el-card>

      <add-dept
        ref="addDept"
        :show-dialog.sync="showDialog"
        :tree-node="node"
        @addDepts="getDepartments"
      />
    </div>
  </div>
</template>

<script>
import treeTools from './components/tree-tools'
import { getDepartments } from '@/api/departments'
import { tranListToTreeData } from '@/utils'
import addDept from './components/add-dept'
export default {
  components: { treeTools, addDept },
  data() {
    return {
      depts: [],
      defaultProps: {
        label: 'name'
      },
      showDialog: false,
      node: {}
    }
  },
  created() {
    this.getDepartments()
  },
  methods: {
    async getDepartments() {
      const result = await getDepartments()
      this.depts = tranListToTreeData(result.depts, '')
    },

    // 删除操作
    async delDepts(id) {
      this.getDepartments()
    },

    // 添加操作
    addDepts(node) {
      this.showDialog = true
      this.node = node
    },

    // 编辑操作
    editDepts(node) {
      this.showDialog = true
      this.node = node
      this.$refs.addDept.getDepartmentsDetail(node.id)
    }
  }
}
</script>

<style scoped>
.tree-code {
  padding: 30px 120px;
  font-size: 14px;
}
.head {
  margin: 10px 0;
}
</style>
